/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.dusa.switco;

/**
 * Structure encapsulating precedence between to tasks.
 * @author vdusa
 */
public class Precedence {
    private Task predecessor;
    private Task successor;
    private Double value = Double.NaN;
    private PrecedenceType type;

    /**
     * Creates a new precedence. 
     * @param predecessor
     * @param successor
     */
    Precedence(Task predecessor, Task successor){
        this.predecessor = predecessor;
        this.successor = successor;
    }

    /**
     * @return the predecessor task
     */
    public Task getPredecessor() {
        return predecessor;
    }

    /**
     * @param predecessor the predecessor task to set
     */
    private void setPredecessor(Task predecessor) {
        this.predecessor = predecessor;
    }

    /**
     * @return the successor task
     */
    public Task getSuccessor() {
        return successor;
    }

    /**
     * @param successor the successor task to set
     */
    private void setSuccessor(Task successor) {
        this.successor = successor;
    }

    /**
     * @return the internal value p_ij (see Problem definition and notation section in the article)
     */
    public Double getValue() {
        return value;
    }

    /**
     * @param value the internal value p_ij (see Problem definition and notation section in the article)
     */
    public void setValue(Double value) {
        this.value = value;
    }

    /**
     * For more information see 
     * @return the type. See {@link PrecedenceType}
     */
    public PrecedenceType getType() {
        return type;
    }

    /**
     * * For more information see 
     * @param type the type to set. See {@link PrecedenceType}
     */
    public void setType(PrecedenceType type) {
        this.type = type;
    }

    /**
     * @return true if the precedence is violated in the schedule. If the
     * successor begins earlier than the predecessors finish.
     */
    public boolean isViolated(){
        if(getPredecessor().getStart() + getValue() > getSuccessor().getStart()){
            return true;
        } else {
            return false;
        }            
    }

    @Override
    public String toString(){
        return "(" + predecessor + "," + successor + ")";
    }
    
    /**
     * Precedence is created by the constructor Precedence(predecessor, successor),
     * so the precedence see these two tasks, but the tasks doesn't see the precedence.
     * In order to make these Tasks to see the precedence is to call this function.
     */
    public void informTasks(){
        getPredecessor().addSuccessor(this);
        getSuccessor().addPredecessor(this);    
    }

    boolean isJoiningSameEqualityTrees(Precedence precedence) {
        return (
                getPredecessor().getEqualityTree() == precedence.getPredecessor().getEqualityTree() && 
                getSuccessor().getEqualityTree() == precedence.getSuccessor().getEqualityTree()
            ) || (
                getPredecessor().getEqualityTree() == precedence.getSuccessor().getEqualityTree() && 
                getSuccessor().getEqualityTree() == precedence.getPredecessor().getEqualityTree()
            );                
    }
}
